package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.common.R;

import com.itheima.pojo.Dish;
import com.itheima.pojo.Setmeal;
import com.itheima.pojo.ShoppingCart;
import com.itheima.service.DishService;
import com.itheima.service.SetmealService;
import com.itheima.service.ShoppingCartService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    /**
     * 回显购物车里的数据
     *
     * @return
     */
    @GetMapping("/list")
    public R selectAll(HttpServletRequest request) {

        Long userId = (Long) request.getSession().getAttribute("userId");

        QueryWrapper<ShoppingCart> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        List<ShoppingCart> list = shoppingCartService.list(wrapper);
        return R.success(list);
    }

    /**
     * 添加购物车
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R AddId(@RequestBody ShoppingCart shoppingCart, HttpServletRequest request) {

        Long userId = (Long) request.getSession().getAttribute("userId");
        if (shoppingCart.getSetmealId() != null) {
            //判断是否为套餐
            QueryWrapper<ShoppingCart> wrapper = new QueryWrapper<>();
            wrapper.eq("user_id", userId);
            wrapper.eq("setmeal_id", shoppingCart.getSetmealId());
            ShoppingCart one = shoppingCartService.getOne(wrapper);
            //查询套餐是否存在
            if (one != null) {
                //存在就修改数量和小计
                one.setNumber(one.getNumber() + 1);
                // one.setAmount(one.getAmount().multiply(new BigDecimal(one.getNumber())));
                shoppingCartService.updateById(one);
            } else {
                //不存在就添加
                shoppingCart.setUserId(userId);
                shoppingCart.setCreateTime(LocalDateTime.now());
                shoppingCartService.save(shoppingCart);
            }
        } else {
            //如果为菜品
            QueryWrapper<ShoppingCart> wrapper = new QueryWrapper<>();
            wrapper.eq("user_id", userId);
            wrapper.eq("dish_id", shoppingCart.getDishId());
            ShoppingCart one = shoppingCartService.getOne(wrapper);
            if (one != null) {
                one.setNumber(one.getNumber() + 1);
                //   one.setAmount(one.getAmount().multiply(new BigDecimal(one.getNumber())));
                shoppingCartService.updateById(one);
            } else {
                shoppingCart.setUserId(userId);
                shoppingCart.setCreateTime(LocalDateTime.now());
                shoppingCartService.save(shoppingCart);
            }

        }

        return R.success(null);
    }

    /**
     * 修改购物车信息
     *
     * @param shoppingCart
     * @param request
     * @return
     */
    @PostMapping("/sub")
    public R deleteId(@RequestBody ShoppingCart shoppingCart, HttpServletRequest request) {

        //获取用户id
        Long userId = (Long) request.getSession().getAttribute("userId");

        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("user_id",userId);

        if (shoppingCart.getDishId() != null) {
            queryWrapper.eq("dish_id", shoppingCart.getDishId());
        }
        if (shoppingCart.getSetmealId() != null) {
            queryWrapper.eq("setmeal_id", shoppingCart.getSetmealId());
        }
        //动态sql查询对应的信息
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);

        for (ShoppingCart cart : list) {
            //如果数量大于1就数量减一
            if(cart.getNumber()>1){
                cart.setNumber(cart.getNumber()-1);
                shoppingCartService.updateById(cart);
                return R.success(null);
            }

            //如果只有一个就直接删除
            shoppingCartService.removeById(cart.getId());
            return R.success(null);
        }
        return R.error("修改失败");
    }

    /**
     * 清空购物车
     * @param request
     * @return
     */
    @DeleteMapping("/clean")
    public R delete(HttpServletRequest request) {

        Long userId = (Long) request.getSession().getAttribute("userId");

        QueryWrapper<ShoppingCart> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        shoppingCartService.remove(wrapper);

        return R.success("删除成功");
    }

}
